<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getList">
        查询
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="handleSave(0)">
        添加
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;">
      <el-table-column label="内容" prop="name" align="center" />
      <el-table-column label="创建时间" prop="createTime" align="center"/>
      <el-table-column label="操作" fixed="right" width="130px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleSave(row.id)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑" v-if="dialogVisible" :visible.sync="dialogVisible" width="800px">
      <el-form ref="postForm" :model="postForm" :rules="rules">
        <el-form-item label="标题" prop="name" >
          <el-col :span="12">
            <el-input v-model.trim="postForm.name" width="200px"/>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submit()">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { articlePageByQo, newsSave, newsRemoveByIds, newsGetById } from '@/api/website'
import waves from '@/directive/waves' // waves directive

export default {
  name: "MicroFastNews",
  directives: { waves },
  props: {

  },
  components: {
    
  },
  data() {
    return {
      list: [],
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        code: 'weiAllNews'
      },
      dialogVisible: false,
      postForm: {
        id: '',
        name: '',
        code: 'weiAllNews',
        gongyingId: ''
      },
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  watch: {},
  methods: {
    getList() {
      this.listLoading = true

      this.listQuery.gongyingId = this.userInfo.id
      articlePageByQo(this.listQuery).then(response => {
        this.list = response.list || []
        this.listLoading = false
      }).catch((err) => {
        console.log(err)
        this.listLoading = false
      })
    },
    handleSave(id) {
      if (id == 0) {
        this.isEdit = false
        this.postForm.id = id
        this.postForm.name = ''
        this.dialogVisible = true
      } else {
        this.isEdit = true
        newsGetById(id).then(response => {
          this.postForm = Object.assign({}, response)
          this.dialogVisible = true
        }).catch(err => {
          console.log(err)
        })
      }
    },
    submit() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          let params = Object.assign({}, this.postForm)
          delete params.createTime
          params.gongyingId = this.userInfo.id
          newsSave(params, this.isEdit).then((res) => {
            this.dialogVisible = false
            this.getList()
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否继续删除？').then(() => {
        newsRemoveByIds({ ids: row.id }).then(() => {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.getList()
        })
      })
    },
  },
  created() {
    this.getList()
  },
  mounted() {
    
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
  
</style>